<template>
  <div>
    <div class="top">
      <p class="t_title"><b>小牛商城</b></p>
      <van-search
        v-model="value"
        shape="round"
        background="rgba(93, 203, 160)"
        placeholder="搜索商品名称"
        @click="search"
      />
    </div>
    <div class="banner_box">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item>
          <img src="../assets/2.png" alt="" />
        </van-swipe-item>
        <van-swipe-item>
          <img src="../assets/3.png" alt="" />
        </van-swipe-item>
        <van-swipe-item>
          <img src="../assets/下载.png" alt="" />
        </van-swipe-item>
      </van-swipe>
      <div class="banner">
        <div>
          <img src="../assets/15.svg" alt="" />
          <p style="color: red"><u>热门推荐</u></p>
        </div>
        <div>
          <img src="../assets/13.svg" alt="" />
          <p>本店热卖</p>
        </div>
        <div>
          <img src="../assets/14.svg" alt="" />
          <p>优选新品</p>
        </div>
        <div>
          <img src="../assets/12.svg" alt="" />
          <p style="margin-top: 2px">网红爆品</p>
        </div>
        <div>
          <img src="../assets/11.svg" alt="" />
          <p style="margin-top: 2px">天天特价</p>
        </div>
      </div>
      <van-pull-refresh v-model="loading" @refresh="onRefresh">
        <div class="cont">
          <div v-for="item in list" :key="item.id" class="cont_a">
            <img :src="item.img" alt="" @click="Xiang(item.id)"/>
            <p style="margin-left: 3px">{{ item.name }}</p>
            <p style="margin-left: 3px; font-size: 12px; color: gainsboro">
              <span
                ><s>{{ item.price_y }}.00</s></span
              >
              <span style="float: right; margin-right: 10px; color: blue">加入购物车</span>
            </p>
            <p style="margin-left: 3px; color: red">
              <span style="font-size: 12px">￥</span> {{ item.price }}.00
              <span style="float: right; margin-right: 10px">
                <button class="c_btn">直接购买</button>
              </span>
            </p>
          </div>
        </div>
      </van-pull-refresh>
    </div>
  </div>
</template>
    
  <script setup lang="ts">
import { onMounted, ref } from 'vue'
import axios from 'axios'
import { showToast } from 'vant';
const value = ref('')
const list = ref([])

// 获取后端数据
const getHome = async () => {
  let {
    data: { data }
  } = await axios.get('http://localhost:3000/dyx/home')
  list.value = data
}

// 点击跳转搜索页面
const search = () => {
  window.location.href = '/search'
}

onMounted(() => {
  getHome()
})

//跳转详情页
const Xiang = (id: number) => {
  window.location.href = '/xiang?id=' + id
}

const loading = ref(false)
const onRefresh = () => {
  // 倒序列表
  list.value = [...list.value].reverse()

  // 模拟下拉刷新延迟
  setTimeout(() => {
    loading.value = false
    showToast('已刷新') // 可以替换为具体的toast实现
  }, 1000)
}
</script>
    
  <style>
* {
  margin: 0;
  padding: 0;
}
.top {
  height: 110px;
  background-color: rgba(93, 203, 160);
  position: fixed;
  width: 100%;
  z-index: 999;
}
.t_title {
  color: white;
  margin-left: 20px;
  line-height: 50px;
}
.my-swipe img {
  width: 95%;
  border-radius: 10px;
  margin: 10px;
}
.banner_box {
  height: 200vh;
  background-color: #f5f5f5;
  padding-top: 110px;
}
.banner {
  width: 95%;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  text-align: center;
}
.banner p {
  font-size: 15px;
}
.cont {
  width: 98%;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
}
.cont img {
  width: 150px;
  height: 145px;
  margin: 15px;
}
.cont_a {
  width: 48%;
  height: 295px;
  background-color: white;
  margin: 3.5px;
  border-radius: 10px;
}
.c_btn {
  border: none;
  background: rgba(225, 57, 64);
  color: white;
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 10px;
}
</style>
  
